jQuery(function() {

// 保存投诉建议信息

    function saveComplain(isreturn){//isreturn==1,直接返回customerindex；isreturn==2在保存产品信息后留着当前页
        var id = jQuery("#complainId").val();
        var customerName=jQuery("#customerName").val();
        var contractID = jQuery('#contractID').val();
        var address = jQuery('#address').val();
        var phone=jQuery('#phone').val();
        var fax = jQuery('#fax').val();
        var contact = jQuery('#contact').val();
        var quality = jQuery("#quality").find("option:selected").text();
        var date = jQuery("#date").find("option:selected").text();
        var afterService = jQuery("#afterService").find("option:selected").text();
        var advice = jQuery('#advice').val();
        var time = jQuery('#time').val();
        var memo = jQuery('#memo').val();
        jQuery(".saveBtn").attr("disabled", true);
        var url = window.rootPath + "/ui/aftersalecomplain/saveComplain";

        // 判断是编辑还是新增
        if (id == "") {
            var d = {
                "customerName" : customerName,
                "contractID" : contractID,
                "address" : address,
                "phone" : phone,
                "fax" : fax,
                "contact" : contact,
                "quality" : quality,
                "date" : date,
                "afterService" : afterService,
                "advice" : advice,
                "time" : time,
                "memo":memo
            };

        } else {
            var d = {
                "id" : id,
                "customerName" : customerName,
                "contractID" : contractID,
                "address" : address,
                "phone" : phone,
                "fax" : fax,
                "contact" : contact,
                "quality" : quality,
                "date" : date,
                "afterService" : afterService,
                "advice" : advice,
                "time" : time,
                "memo":memo
            };
        }
        jQuery.ajax({
            type: 'post',
            url:  encodeURI(url),
            data: JSON.stringify(d),
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                if(isreturn==1) {
                    window.location = window.rootPath + "/ui/aftersalecomplain";
                    jQuery(".saveBtn").attr("disabled", false);
                }
                else if(isreturn==2){
                    var complainId=data.complainId;
                    var time=moment(data.createTime).format("YYYY-MM-DD HH:mm:ss")
                    jQuery("#complaincreateTime").val(time);
                    saveProMess(complainId);
                    jQuery(".saveBtn").attr("disabled", false);
                }
                else if(isreturn=='uploadBtn'){
                    var newsId = data.complainId;
                    jQuery('#complainId').val(newsId);
                    fileUpload(newsId, "投诉建议附件",null,'attachmentnumber');}
                    jQuery(".saveBtn").attr("disabled", false);

            }
        });
    }


    jQuery('.saveBtn').on('click', function() {
        if (jQuery("#complainAdd") && !jQuery("#complainAdd").Validform({
                tiptype : 3,
                showAllError : true
            }).check(false)) {
            return;
        };
        saveComplain(1);
    });

///保存产品信息//////////
    function saveProMess(complainId) {
        var id = jQuery("#proId").val();
        var orderTime = jQuery("#orderTime").val();
        var typeSpecification = jQuery("#typeSpecification").val();
        var number = jQuery("#number").val();
        jQuery(".proSaveBtn").attr("disabled", true);
        var url1 = window.rootPath + "/ui/aftersalecomplain/saveProMess";
        var d;
        if(id==''){
            d = {
                "orderTime": orderTime,
                "typeSpecification": typeSpecification,
                "number": number,
                "complain": {
                    "id": complainId
                }
            };
            jQuery.ajax({
                type: 'post',
                url: encodeURI(url1),
                data: JSON.stringify(d),
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    jQuery('#proMessModal').modal('hide');
                    proMessData.complainId=complainId;
                    jQuery('#complainId').val(complainId);
                    proMessTable.draw();
                    jQuery(".proSaveBtn").attr("disabled", false);
                }
            });
        }else{
            d = {
                "id":id,
                "orderTime": orderTime,
                "typeSpecification": typeSpecification,
                "number": number,
                "complain": {
                    "id": complainId
                }
            };
            jQuery.ajax({
                type: 'post',
                url:  encodeURI(url1),
                data: JSON.stringify(d),
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    jQuery('#proMessModal').modal('hide');
                    jQuery('#complainId').val(complainId);
                    proMessTable.draw(false);
                    jQuery(".proSaveBtn").attr("disabled", false);
                }
            });
        }
    }

////获得产品信息 服务器分页
    var proMessData = {};
    var proid=jQuery('#complainId').val();
    proMessData.complainId=proid;
    var proMessTable;
    jQuery(document).ready(function () {
        proMessTable = jQuery('#accessTable').DataTable({
            "serverSide": true,
            "iDisplayLength":5,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/aftersalecomplain/getProMess',
                "data": function (d) {
                    return $.extend({}, d, proMessData);
                }
            } ,
            columns: [
                {
                    data: null,
                    defaultContent: ""//排序
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var id=data.id
                        var operate ='<input type="checkbox" class="checkchild">';
                        return operate;
                    }
                },
                {
                    data: 'orderTime',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'typeSpecification'
                },
                {
                    data: 'number'
                },
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    });

///////列表中的全选
    $("#checkAll").click(function(){
        var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
    });

    ////////////编辑关键人物///////
    jQuery('.proEditBtn').on('click', function () {
        var selects=getSelect("#accessTable",1);
        if(selects.length>1){
            $.springLemonDialog({
                content: '只能编辑一条记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }
        else if(selects.length==0){
            $.springLemonDialog({
                content: '请选择要编辑的记录',// 确认文本
                clickButton: function (sender, modal, index) {// 按钮事件处理
                    $(this).closeDialog(modal);
                }
            });
        }else if(selects.length==1){
            jQuery("#proId").val(selects[0].id);
            jQuery("#orderTime").val(selects[0].orderTime);
            jQuery("#typeSpecification").val(selects[0].typeSpecification);
            jQuery('#number').val(selects[0].number);
            jQuery('#proMessModal').modal('show');
        }
    });

    ////////删除产品信息//////
    jQuery('.proDelBtn').on('click', function (){
        var selects=getSelect("#accessTable",1);
        var url=window.rootPath+"/ui/aftersalecomplain/delProMess";
        delSelect(selects,url,proMessTable)
    });

//新增产品信息
    jQuery(".proAddBtn").on("click",function(){
        var id=jQuery('#complainId').val();
        if (jQuery("#complainAdd") && !jQuery("#complainAdd").Validform({
                tiptype : 3,
                showAllError : true
            }).check(false)) {
            return;
        };
        jQuery("#proId").val('');
        jQuery("#orderTime").val('');
        jQuery("#typeSpecification").val('');
        jQuery('#number').val('');
        jQuery("#proMessModal").modal('show');
    });

//保存产品信息
    jQuery('.proSaveBtn').on('click',function(){
        if (jQuery("#proMessEdit") && !jQuery("#proMessEdit").Validform({
                tiptype : 3,
                showAllError : true
            }).check(false)) {
            return;
        };
        saveComplain(2);
    })

    // 上传附件
    jQuery(".uploadBtn").on('click', function() {
        var complainId = jQuery("#complainId").val();
        if (complainId == "") {
            if (jQuery("#ComplainAdd") && !jQuery("#complainAdd").Validform({
                    tiptype : 3,
                    showAllError : true
                }).check(false)) {
                return;
            }
            ;
           saveComplain('uploadBtn'); 
//              $.springLemonDialog({
//                content : '将保存附件，是否继续',// 确认文本
//                clickButton : function(sender, modal, index) {// 按钮事件处理
//                if (index == 0) {
//                saveComplain('uploadBtn');
//              }
//                 $(this).closeDialog(modal);
//            }
//             });
        } else {
            fileUpload(complainId, "投诉建议附件",null,'attachmentnumber');
        }
    });
    // 下载附件
          //jQuery(".downloadBtn").on('click', function() {
          //var selects=getSelect("#accessTable",1);
          //var newsId=selects[0].complainId;
          //filedown(newsId, '投诉建议附件');
      //});
          jQuery(".downloadBtn").on('click', function() {
      		var complainId = jQuery("#complainId").val();
      		filedown(complainId, '投诉建议附件','1');
      	});
          
          ////获附件数量
      	var complainId = jQuery("#complainId").val();
      	var numId='attachmentnumber';
      	if(complainId !=''){
      		showANum(complainId,'投诉建议附件',numId);
      	}

});

